package com.mql.gre.rs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import com.mql.gre.beans.Document;
import com.mql.gre.beans.DocumentsList;
import com.mql.gre.beans.Produit;
import com.mql.gre.business.DocumentManagerImpl;

@Path("/documents")
public class DocumentManagerRS {

	private DocumentManagerImpl dm;

	private List<Produit> ds;

	public DocumentManagerRS(DocumentManagerImpl dm) {
		super();
		this.dm = dm;
	}

	@GET
	@Path("text/{id}")
	@Produces("application/xml")
	public String getDocumentByID(@PathParam("id") String id)
			throws JAXBException {
		Document document = dm.getDocumentByID(id);
		if (document == null) {
			throw new WebApplicationException(Response.Status.NOT_FOUND);
		}

		JAXBContext jxb = JAXBContext.newInstance(Document.class);
		StringWriter writer = new StringWriter();
		jxb.createMarshaller().marshal(document, writer);
		return writer.toString();
	}

	@GET
	@Path("xml/{id}")
	@Produces("application/xml")
	public String getDocument(@PathParam("id") String id)
			throws FileNotFoundException {
		Document document = dm.getDocumentByID(id);
		if (document == null) {
			throw new WebApplicationException(Response.Status.NOT_FOUND);
		}

		return document.toXML();
	}

	@GET
	@Path("prod")
	@Produces(MediaType.APPLICATION_XML)
	public Produit getProduit(@PathParam("id") String id) throws JAXBException {
		Produit p = new Produit("ref01", "Clavier", 20., 15.);
//		JAXBContext jxb = JAXBContext.newInstance(Produit.class);
//		StringWriter writer = new StringWriter();
//		jxb.createMarshaller().marshal(p, writer);
		return p;
	}

	@GET
	@Path("all")
	@Produces(MediaType.APPLICATION_XML)
	public String getAll(@PathParam("id") String id) throws JAXBException {

		List<Document> list = new ArrayList<Document>();

		list.add(dm.getDocumentByID("001"));
		list.add(dm.getDocumentByID("002"));

		DocumentsList listd = new DocumentsList(list);
		
		JAXBContext jxb = JAXBContext.newInstance(DocumentsList.class);

		StringWriter writer = new StringWriter();
		jxb.createMarshaller().marshal(listd, writer);

		return writer.toString();
	}

	// Ecriture XML de l'objet document dans le OutputStream
	public void outputDocument(OutputStream outputStream, Document d) {
		PrintStream writer = new PrintStream(outputStream);
		writer.println("<document id=\"" + d.getId() + "\">");
		writer.println("    <title>" + d.get("title") + "</title>");
		writer.println("    <author>" + d.get("author") + "</author>");
		writer.println("</document>");
	}

	// Implementation de l'interface StreamingOutput
	private class StreamingOutPutImp implements StreamingOutput {

		private Document document;

		public StreamingOutPutImp(Document document) {
			this.document = document;
		}

		public void write(OutputStream outputStream) throws IOException,
				WebApplicationException {
			outputDocument(outputStream, document);

		}

	}

}
